package com.nobody.facepk.view;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.view.AbstractView;


public class TextView extends AbstractView {
	public static final String PLAIN_CONTENT_TYPE = "text/plain;charset=utf-8";
	
	private final Logger logger = LoggerFactory.getLogger(TextView.class);

	public TextView() {
		super.setContentType(PLAIN_CONTENT_TYPE);
	}

	@Override
	protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.setContentType(PLAIN_CONTENT_TYPE);
		response.setCharacterEncoding("utf-8");
		
		String text = (String)model.get("model");
		
		try {
			response.getWriter().print(text);
			
		} catch (Exception e) {
			if (this.logger.isDebugEnabled()) {
				StringBuilder buf = new StringBuilder();
				buf.append("textView").append(" ").append(request.getRequestURI());
				if (request.getQueryString() != null)
					buf.append("?").append(request.getQueryString());
				this.logger.debug(buf.toString(), e);
			}
			throw e;
		}finally {
			if (this.logger.isDebugEnabled()) {
				StringBuilder buf = new StringBuilder();
				buf.append(text.length()).append(" ").append(request.getRequestURI());
				if (request.getQueryString() != null)
					buf.append("?").append(request.getQueryString());
				buf.append("\r\n");
				buf.append(text);
				buf.append("\r\n");
				buf.append("\r\n");
				this.logger.debug(buf.toString());
			}
		}
	}
	
	
	
	
	
}
